package com.zhisheng.gufei.web.action.produce;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.demo2do.lighturl.annotation.Action;
import com.zhisheng.core.utils.DateUtils;
import com.zhisheng.core.web.action.BaseAction;
import com.zhisheng.gufei.city.entity.City;
import com.zhisheng.gufei.city.service.CityService;
import com.zhisheng.gufei.grabagesort.entity.GrabageSort;
import com.zhisheng.gufei.grabagesort.service.GrabageSortService;
import com.zhisheng.gufei.produce.entity.BaseInformation;
import com.zhisheng.gufei.produce.entity.Declaration;
import com.zhisheng.gufei.produce.entity.GrabageFacilities;
import com.zhisheng.gufei.produce.entity.GrabageProduceDestInfo;
import com.zhisheng.gufei.produce.entity.MainProduce;
import com.zhisheng.gufei.produce.service.BaseInformationService;
import com.zhisheng.gufei.produce.service.DeclarationService;
import com.zhisheng.gufei.produce.service.FacilitiesService;
import com.zhisheng.gufei.produce.service.GrabageProduceDestInfoService;
import com.zhisheng.gufei.utils.Constants;

/**
 * To handle the declaration, save and require as the main part
 * 
 * @author xgye
 */
public class DeclarationAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private DeclarationService declarationService;
	@Resource 
	private BaseInformationService baseInfoService;
	@Resource
	private GrabageProduceDestInfoService grabageProduceDestInfoService;
	@Resource
	private FacilitiesService facilitiesService;
	
	@Resource 
	private CityService cityService;
	@Resource
	private GrabageSortService grabageSortService;
	
	
	private Declaration dec;
	private BaseInformation info;
	
	/** 表示处理设施的表格 */
	private List<GrabageFacilities> facList;
	private List<GrabageProduceDestInfo> produceList;
	
	private List<City> allCity;
	private List<GrabageSort> grabageList;
	
	private List<MainProduce> mainProduceList;
	private List<MainProduce> mainConsumeList;
	
	private boolean canAudit;
	
	@Action("/declaration/save")
	public String save() {
	    dec.setCreatedTime(new Date());
	    dec.setStatus(1);
	    declarationService.saveDeclaration(dec);
	    
	    String mainProduce = declarationService.populateToString(mainProduceList);
	    info.setMainProduceInfo(mainProduce);
	    
	    String mainConsume = declarationService.populateToString(mainConsumeList);
        info.setMainProduceInfo(mainConsume);
	    
	    info.setCorperation(dec);
	    baseInfoService.saveBaseInfo(info);
	    
	    for (GrabageFacilities f : facList) {
	        f.setDeclaration(dec);
	        facilitiesService.saveFacilities(f);
	    }
	    
	    for (GrabageProduceDestInfo pd : produceList) {
	        pd.setCorperation(dec);
	        grabageProduceDestInfoService.saveProduceDestInfo(pd);
	    }
	    return "r:/produce/audit/waiting";
	}
	
	@Action("/declaration/add")
	public String add() {
		dec = new Declaration();
		dec.setDeclareYear(DateUtils.getYear(new Date()));
		
	    allCity = cityService.getAllCity();
	    grabageList = grabageSortService.findAll(GrabageSort.class);
	    
	    mainProduceList = declarationService.getInitialProduceList(1);
	    mainConsumeList = declarationService.getInitialProduceList(2);
	    
	    produceList = grabageProduceDestInfoService.getInitialInfoList();
	    facList = facilitiesService.getInitialFacilities();
	    
	    return "add";
	}
	
	@Action("/declaration/view")
	@SuppressWarnings("unchecked")
	public String view() {
		dec = declarationService.findById(dec.getId());
		Set<String> auths = getCurrentUser().getRoleAuthoritiesSet();
		if(auths.contains("ROLE_ADMIN")){
			canAudit = false;
		}else if(auths.contains("ROLE_AUDIT_T") ){
			if(dec.getStatus() > 0 && dec.getStatus() < Constants.Audit.AUDIT_T){
				canAudit = true;
			}
		}else if(auths.contains("ROLE_AUDIT_C")){
			if(dec.getStatus() > 0 && dec.getStatus() < Constants.Audit.AUDIT_C){
				canAudit = true;
			}
		}else if(auths.contains("ROLE_AUDIT_P")){
			if(dec.getStatus() > 0 && dec.getStatus() < Constants.Audit.AUDIT_P){
				canAudit = true;
			}
		}else{
			createPage();
		}
		
		allCity = cityService.getAllCity();
        return "view";
	}
	
    public Declaration getDec() {
        return dec;
    }

    public void setDec(Declaration dec) {
        this.dec = dec;
    }

    public BaseInformation getInfo() {
        return info;
    }

    public void setInfo(BaseInformation info) {
        this.info = info;
    }

    public List<City> getAllCity() {
        return allCity;
    }

    public void setAllCity(List<City> allCity) {
        this.allCity = allCity;
    }

    public List<GrabageSort> getGrabageList() {
        return grabageList;
    }

    public void setGrabageList(List<GrabageSort> grabageList) {
        this.grabageList = grabageList;
    }

    public List<MainProduce> getMainProduceList() {
        return mainProduceList;
    }

    public void setMainProduceList(List<MainProduce> mainProduceList) {
        this.mainProduceList = mainProduceList;
    }

    public List<GrabageFacilities> getFacList() {
        return facList;
    }

    public void setFacList(List<GrabageFacilities> facList) {
        this.facList = facList;
    }

    public List<MainProduce> getMainConsumeList() {
        return mainConsumeList;
    }

    public void setMainConsumeList(List<MainProduce> mainConsumeList) {
        this.mainConsumeList = mainConsumeList;
    }

    public List<GrabageProduceDestInfo> getProduceList() {
        return produceList;
    }

    public void setProduceList(List<GrabageProduceDestInfo> produceList) {
        this.produceList = produceList;
    }
    
	public boolean isCanAudit() {
		return canAudit;
	}

	public void setCanAudit(boolean canAudit) {
		this.canAudit = canAudit;
	}
	
}
